package org.dromara.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import org.dromara.app.appReturn.AppMemberCenterReturn;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.app.domain.bo.SkVipPriceBo;
import org.dromara.app.domain.vo.SkVipPriceVo;
import org.dromara.app.domain.SkVipPrice;
import org.dromara.app.mapper.SkVipPriceMapper;
import org.dromara.app.service.ISkVipPriceService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 开通会员价格Service业务层处理
 *
 * @author Evacuation
 * @date 2024-07-23
 */
@RequiredArgsConstructor
@Service
public class SkVipPriceServiceImpl implements ISkVipPriceService {

    private final SkVipPriceMapper baseMapper;

    /**
     * 查询开通会员价格
     *
     * @param id 主键
     * @return 开通会员价格
     */
    @Override
    public SkVipPriceVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询开通会员价格列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 开通会员价格分页列表
     */
    @Override
    public TableDataInfo<SkVipPriceVo> queryPageList(SkVipPriceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkVipPrice> lqw = buildQueryWrapper(bo);
        Page<SkVipPriceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的开通会员价格列表
     *
     * @param bo 查询条件
     * @return 开通会员价格列表
     */
    @Override
    public List<SkVipPriceVo> queryList(SkVipPriceBo bo) {
        LambdaQueryWrapper<SkVipPrice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkVipPrice> buildQueryWrapper(SkVipPriceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkVipPrice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, SkVipPrice::getType, bo.getType());
        lqw.eq(bo.getPrice() != null, SkVipPrice::getPrice, bo.getPrice());
        lqw.eq(bo.getOldPrice() != null, SkVipPrice::getOldPrice, bo.getOldPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), SkVipPrice::getTitle, bo.getTitle());
        return lqw;
    }

    /**
     * 新增开通会员价格
     *
     * @param bo 开通会员价格
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SkVipPriceBo bo) {
        SkVipPrice add = MapstructUtils.convert(bo, SkVipPrice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改开通会员价格
     *
     * @param bo 开通会员价格
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SkVipPriceBo bo) {
        SkVipPrice update = MapstructUtils.convert(bo, SkVipPrice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkVipPrice entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除开通会员价格信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 会员中心
     *
     * @param req
     * @return
     */
    @Override
    public List<AppMemberCenterReturn> getMemberCenter(HttpServletRequest req) {
        List<AppMemberCenterReturn> list = new ArrayList<>();
        List<SkVipPrice> listprice = baseMapper.selectList(
            new QueryWrapper<SkVipPrice>().lambda()
                .orderByAsc(SkVipPrice::getPrice)
        );
        list = BeanUtil.copyToList(listprice, AppMemberCenterReturn.class);
        return list;
    }
}
